package com.djx.plus.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.djx.comm.util.StringUtils;
import com.djx.plus.dao.BookMapper;
import com.djx.plus.entity.Book;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author PACTERA-468
 */
@Service
public class BookService {

    @Resource
    BookMapper bookMapper;

    public List<Book> queryList(Book book) {
        QueryWrapper<Book> wrapper = getWrapper(book);
        return bookMapper.selectList(wrapper);
    }

    public Integer insert(Book book) {
        return bookMapper.insert(book);
    }

    public Page queryPage(Book book, int start, int end) {
        QueryWrapper<Book> wrapper = getWrapper(book);
        Page page = new Page(start, end);
        return bookMapper.selectPage(page, wrapper);
    }

    public Book queryById(String id) {
        return bookMapper.selectById(id);
    }

    private QueryWrapper getWrapper(Book book) {
        QueryWrapper<Book> wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(book.getName())) {
            wrapper.lambda().eq(Book::getName, book.getName());
        }
        if (null != book.getPrice()) {
            wrapper.lambda().eq(Book::getPrice, book.getPrice());
        }
        if (StringUtils.isNotBlank(book.getAuthor())) {
            wrapper.lambda().eq(Book::getAuthor, book.getAuthor());
        }
        return wrapper;
    }
}
